package com.liunian.algorithmstudy.tree;

public class IsBalanced110 extends TreeNode {

	public boolean isBalanced(TreeNode root) {
		return travelsal(root) != -1;
	}

	private int travelsal(TreeNode root) {
		if (root == null) {
			return 0;
		}
		int l = travelsal(root.left);
		if (l == -1) {
			return -1;
		}
		int r = travelsal(root.right);
		if (r == -1) {
			return -1;
		}
		if (Math.abs(r - l) > 1) {
			return -1;
		} else {
			return Math.max(r, l) + 1;
		}
	}

	public static void main(String[] args) {
		TreeNode treeNode4 = new TreeNode(4, null, null);
		TreeNode treeNode5 = new TreeNode(5, null, null);
		TreeNode treeNode6 = new TreeNode(6, null, null);
		TreeNode treeNode2 = new TreeNode(2, treeNode4, treeNode5);
		TreeNode treeNode3 = new TreeNode(3, treeNode6, null);
		TreeNode treeNode1 = new TreeNode(1, treeNode2, treeNode3);
		IsBalanced110 test = new IsBalanced110();
		System.out.println(test.isBalanced(treeNode1));
	}

}
